package com.example.result

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.result.dao.api.DaoApi
import com.example.result.dao.db.DaoDb
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.MatcherAssert
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)

class DaodejingDbTest : DbTest<DaoDb>() {
    @get:Rule
    var instantTaskExecutorRule = InstantTaskExecutorRule()

    @Test
    fun insertAndLoad() {
        val data = DaoApi.create("12".toHttpUrlOrNull()!!).getData(1).execute().body()
        data?.data?.apply {
            db.sectionDao().insertSection(this)
//            var section = this[0]


            for (section in this) {
                MatcherAssert.assertThat(
                    db.sectionDao().getSection(section.id),
                    `is`(section)
                )
                db.contentDao().insert(section.data.map {
                    it.sectionId = section.id
                    it
                })
                MatcherAssert.assertThat("insert", db.contentDao().get(section.id).isNotEmpty())
                print(db.sectionDao().getSection(section.id).name)
                print(db.contentDao().get(section.id))
                db.sectionDao().deleteSection(section)
                MatcherAssert.assertThat("insert", db.contentDao().get(section.id).isEmpty())
            }
        }

    }

    override fun requireDb(): DaoDb {
        return DaoDb.create(ApplicationProvider.getApplicationContext(), true)
    }
}